<template>
  <t-dialog
    v-model:visible="formVisible"
    header="新增或编辑系统角色"
    width="680"
    :footer="false"
    @close="onClickCloseBtn"
  >
    <template #body>
      <!-- 表单内容 -->
      <t-form ref="form" :data="formData" :rules="rules" :label-width="100" @submit="onSubmit">
        <t-input v-model="formData.roleId" hidden></t-input>
        <t-form-item label="名称" name="roleName">
          <t-input v-model="formData.roleName" />
        </t-form-item>
        <t-form-item label="属性值" name="roleCode">
          <t-input v-model="formData.roleCode" />
        </t-form-item>
        <t-form-item label="权限" name="authorityTree">
          <t-tree-select v-model="formData.authorityTree" multiple :data="selectTree" placeholder="请选择" />
        </t-form-item>
        <t-form-item style="float: right">
          <t-button variant="outline" @click="onClickCloseBtn">取消</t-button>
          <t-button theme="primary" type="submit">确定</t-button>
        </t-form-item>
      </t-form>
    </template>
  </t-dialog>
</template>

<script setup lang="tsx">
import { FormRules, MessagePlugin, SubmitContext } from 'tdesign-vue-next';
import { onMounted, PropType, ref, watch } from 'vue';

import { post, saveOrUpdate } from '@/api/common';

export interface FormData {
  roleId: number;
  roleName: string;
  roleCode: string;
  authorityTree: [];
}

const INITIAL_DATA: FormData = {
  roleId: null,
  roleName: '',
  roleCode: '',
  authorityTree: [],
};

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: Object as PropType<FormData>,
});

const selectTree = ref([
  {
    label: '广东省',
    value: 'guangdong',
    children: [
      {
        label: '广州市',
        value: 'guangzhou',
      },
      {
        label: '深圳市',
        value: 'shenzhen',
      },
    ],
  },
  {
    label: '江苏省',
    value: 'jiangsu',
    children: [
      {
        label: '南京市',
        value: 'nanjing',
      },
      {
        label: '苏州市',
        value: 'suzhou',
      },
    ],
  },
]);

onMounted(async () => {
  const select = await post('/sysRoute/selectTree', {});
  console.log('r', select);
  selectTree.value = select;
});

const formVisible = ref(false);
const formData = ref({ ...INITIAL_DATA });

const onSubmit = async ({ validateResult, firstError }: SubmitContext) => {
  if (!firstError) {
    await saveOrUpdate('/sysRole/saveOrUpdate', formData.value);
    MessagePlugin.success('提交成功');
    formVisible.value = false;
    // await emit.refreshTable();
    await emit('refreshTable');
  } else {
    console.log('Errors: ', validateResult);
    MessagePlugin.warning(firstError);
  }
};

const onClickCloseBtn = () => {
  formVisible.value = false;
  formData.value = { ...INITIAL_DATA };
  console.log(`onClickCloseBtn`);
};

const emit = defineEmits(['update:visible', 'refreshTable']);
// const emit = defineEmits<{
//   'update:visible': void;
//   refreshTable: void;
// }>();

watch(
  () => formVisible.value,
  (val) => {
    emit('update:visible', val);
  },
);

watch(
  () => props.visible,
  (val) => {
    formVisible.value = val;
  },
);

watch(
  () => props.data,
  (val) => {
    formData.value = val;
  },
);

const rules: FormRules = {
  roleName: [{ required: true, message: '请输入名称', type: 'error' }],
  roleCode: [{ required: true, message: '请输入代码', type: 'error' }],
  authorityTree: [{ required: true, message: '请选择权限', type: 'error' }],
};
</script>
